<?php

namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Comm\CommonController as common;
use App\Http\model\Dictionary;

class Purchcar extends Model
{
	/*
    |--------------------------------------------------------------------------
    |限号处理部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addNumberModel 添加限号规则]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [数据] arr $data
    * @version  6.2
    * @param    [type]     $data [description]
    */
	public function addNumberModel($data){
		$data['reset_person'] = session('username');
		$data['update_time'] = date('Y-m-d H:i:s');
		$add = Db::table('scope')
				->insert($data);
		if ($add) {
			
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}
		return json_encode($result);
	}

	/**
	 * [findNumberModel 限号列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [查询条件] $where
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function findNumberModel($where){
		$result = array();

		$fields = array(
				'scope.*',
				'region.fullname as areaname'
			);

		$scopeObj = Db::table('scope')
						->join('region','region.code','=','scope.area')
						->where($where)
						->orderBy('update_time', 'desc')
						->select($fields)
						->get();
		$scopeArr = json_decode(json_encode($scopeObj),true);
		foreach ($scopeArr as $key => $value) {
			$scopeArr[$key]['rule'] = json_decode($value['rule'],true);

		}
		if ($scopeArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $scopeArr;

		}else{

			$result['code'] = 1004;
			$result['msg'] = '没有数据';
		}

		return json_encode($result);

	}

	/**
	 * [deleteNumberModel 删除限号]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [限号id] arr $numberId
 	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteNumberModel($numberId){

		$deleteScope = Db::table('scope')
						->whereIn('id', $numberId)
						->delete();
		if ($deleteScope) {
			
			$result['code'] = 1000;
    		$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}

		return json_encode($result);
	}

	/**
	 * [updateNumberModel 修改限号]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateNumberModel($data,$numberId){
		$data['reset_person'] = session('username');
		$data['update_time'] = date('Y-m-d H:i:s');
		$update = Db::table('scope')
				->where('id',$numberId)
				->update($data);
		if ($update) {
			
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}
		return json_encode($result);
	}
	/*
    |--------------------------------------------------------------------------
    |厂商处理部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addSuppModel 增加厂商]
    * @Author   孙丽男
    * @DateTime 2018-06-07
    * @param    [数据] arr $data
    * @version  6.2
    */
	public function addSuppModel($data){

		$data['reset_person'] = session('username');
		$data['create_time'] = date('Y-m-d H:i:s');
		$add = Db::table('supplier')
				->insert($data);
		if ($add) {
			
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}
		return json_encode($result);
	}

	/**
	 * [findSuppModel 厂商列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @version  6.2
	 * @return   [json]     [$result]
	 */
	public function findSuppModel($where){

		$result = array();

		$suppObj = Db::table('supplier')
						->where($where)
						->orderBy('create_time', 'desc')
						->get();
		$suppArr = json_decode(json_encode($suppObj),true);

		if ($suppArr) {
			
			$fields = array(
					'purchase.*',
					'car_type.name as typename',
					'car_type.id as typeid',
					'car_series.name as seriesname',
					'car_series.id as seriesid'
				);

			//查询采购商
			foreach ($suppArr as $key => $value) {

				$purchaseObj = Db::table('purchase')
						->where('supp_id',$value['id'])
						->join('car_type','car_type.id','=','purchase.car_id')
						->join('car_series','car_series.id','=','car_type.sid')
						->select($fields)
						->get();
				$purchaseArr = json_decode(json_encode($purchaseObj),true);

				$suppArr[$key]['purch'] = $purchaseArr;

			}
			//计算总价,处理数据
			foreach ($suppArr as $key => $value) {
				$money = 0;
				$npurch = '';
				foreach ($value['purch'] as $k => $v) {
					$money += $v['price']*$v['number'];
					$npurch .= '['.$v['seriesname'].' '.$v['typename'].']'.' '.$v['number'].'台 '.$v['price'].'元 '.$v['time'].'/';
				}
				$suppArr[$key]['money'] = $money;
				$suppArr[$key]['purch'] = $npurch;
			}
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $suppArr;

		}else{

			$result['code'] = 1004;
			$result['msg'] = '没有数据';
		}

		return json_encode($result);
	}

	/**
	 * [deleteSuppModel 删除厂商]
	 * @Author   孙丽男
	 * @DateTime 2018-06-07
	 * @param    [厂商id] arr $suppIdArr
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function deleteSuppModel($suppIdArr){

		$deleteSupp = Db::table('supplier')
						->whereIn('id', $suppIdArr)
						->delete();
		if ($deleteSupp) {
			
			$result['code'] = 1000;
    		$result['msg'] = '删除成功';

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}

		return json_encode($result);
	}

	/**
	 * [updateSuppModel 修改厂商]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updateSuppModel($data ,$suppId){

		$data['reset_person'] = session('username');
		$data['create_time'] = date('Y-m-d H:i:s');
		$update = Db::table('supplier')
				->where('id',$suppId)
				->update($data);
		if ($update) {
			
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}
		return json_encode($result);
	}

	/*
    |--------------------------------------------------------------------------
    |采购处理部分
    |--------------------------------------------------------------------------
    */
	/**
	 * [addPurchModel 添加采购]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [数据] arr $data
	 * @version  6.2
	 */
	public function addPurchModel($data){

		$add = Db::table('purchase')
				->insert($data);
		if ($add) {
			
			$result['code'] = 1000;
			$result['msg'] = '添加成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}
		return json_encode($result);

	}

	/**
	 * [findPurchModel 采购列表]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [厂商id] int $suppId
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function findPurchModel($where){

		$fields = array(
				'purchase.id',
				'purchase.number',
				'purchase.price',
				'purchase.time',
				'car_type.name as typename',
				'car_series.name as seriesname',

			);
		$purchObj = Db::table('purchase')
						->where($where)
						->join('car_type','car_type.id','=','purchase.car_id')
						->join('car_series','car_series.id','=','car_type.sid')
						->select($fields)
						->orderBy('purchase.time', 'desc')
						->get();
		$purchArr = json_decode(json_encode($purchObj),true);

		if ($purchArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $purchArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		}

		return json_encode($result);
	}

	/**
	 * [updatePurchModel 修改采购]
	 * @Author   孙丽男
	 * @DateTime 2018-06-08
	 * @param    [type]
	 * @version  6.2
	 * @return   [type]     [description]
	 */
	public function updatePurchModel($data,$purchId){
		$data['time'] = date('Y-m-d H:i:s');
		$update = Db::table('purchase')
				->where('id',$purchId)
				->update($data);
		if ($update) {
			
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}
		return json_encode($result);
	}
	/*
    |--------------------------------------------------------------------------
    |平台车辆处理部分
    |--------------------------------------------------------------------------
    */
   /**
    * [addCarModel 添加平台车辆]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [数据] arr $data
    * @param  	[保险数据] $insura
    * @version  6.2
    */
  	public function addCarModel($data,$insura){

  		$carObj = Db::table('car_car')
					->where('num',$data['num'])
					->select('id')
					->first();
		if ($carObj) {
			$result['code'] = 1005;
			$result['msg'] = '数据已存在';
			return $result;
		}
  		$data['modifier'] = session('username');
		$data['modtime'] = date('Y-m-d H:i:s');
		$carid = Db::table('car_car')
				->insertGetId($data);
		if ($carid) {
			//添加保险
			foreach ($insura as $key => $value) {
				$insura[$key]['carid'] = $carid;
			}
			$add = Db::table('car_insurance')
				->insert($insura);
			if (!$add) {
				$result['code'] = 1004;
				$result['msg'] = '保险添加失败';
			}else{
				$result['code'] = 1000;
				$result['msg'] = '添加成功';
			}
				
		}else{

			$result['code'] = 1004;
			$result['msg'] = '添加失败';
		}
		return $result;
  	}	
  	/**
  	 * [findCarModel 平台车辆列表]
  	 * @Author   孙丽男
  	 * @DateTime 2018-06-08
  	 * @param    [查询条件] arr $where
  	 * @version  6.2
  	 * @return   [type]     [description]
  	 */
  	public function findCarModel($where){

  		$result = array();
  		//查询车辆信息
  		if (!isset($where[0])) {
  			$fields = array(

  				'car_car.id',
  				'car_car.num',
  				'car_car.imei',
  				'car_car.color',
  				'car_car.status',
  				'car_car.modifier',
  				'car_car.modtime',
  				'region.fullName',
  				'region.code as regioncode',
  				'region.parentCode',
  				'car_type.name as typename',
  				'driver.driver_name as drivername',
  				'driver.id as driverid',
  				'convoy.name as convoyname',
  				'branch.company_name as companyname',
  				'branch.area',
  				'scope.rule',
  				'scope.start_time as starttime',
  				'scope.end_time as endtime',

  			);
  		}else{

  			$fields = array(

  				'car_car.*',
  				'region.fullName',
  				'region.code as regioncode',
  				'region.parentCode',
  				'car_type.name as typename',
  				'driver.driver_name as drivername',
  				'driver.id as driverid',
  				'convoy.name as convoyname',
  				'branch.company_name as companyname',
  				'branch.area',
  				'scope.rule',
  				'scope.start_time as starttime',
  				'scope.end_time as endtime',

  			);
  		}

		$carObj = Db::table('car_car')
						->where($where)
						->join('car_type','car_type.id','=','car_car.typeid')
						->join('region','region.code','=','car_car.area')
						->leftJoin('driver','driver.car_id','=','car_car.id')
						->leftJoin('convoy','convoy.id','=','driver.convoy_id')
						->join('branch','branch.id','=','convoy.branch_id')
						->leftJoin('scope','scope.area','=','car_car.area')
						->select($fields)
						->orderBy('car_car.modtime', 'desc')
						->get();
		
		$carArr = json_decode(json_encode($carObj),true);

		$zidian = new Dictionary;

		if ($carArr) {

			foreach ($carArr as $key => $value) {

				if ($value['subsidiary']) {
					$carArr[$key]['subsidiary'] = $zidian->dictionary($value['subsidiary']);
				}

				if ($value['attribute']) {
					$carArr[$key]['attribute'] = $zidian->dictionary($value['attribute']);
				}
				if ($value['due']) {
					$carArr[$key]['due'] = $zidian->dictionary($value['due']);
				}

				$areaObj = Db::table('region')
						->where('code',$value['parentCode'])
						->first();
				$areaArr = json_decode(json_encode($areaObj),true);
				$carArr[$key]['fullName'] = $areaArr['fullName'].'/'.$value['fullName'];
				$carArr[$key]['parentCode'] = $areaArr['parentCode'];
			}

			foreach ($carArr as $key => $value) {
				
				$areaObj = Db::table('region')
						->where('code',$value['parentCode'])
						->first();
				$areaArr = json_decode(json_encode($areaObj),true);
				$carArr[$key]['fullName'] = $areaArr['fullName'].'/'.$value['fullName'];
			}
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $carArr;

		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		}

		return json_encode($result);
  	}

  	/**
  	 * [deleteCarModel 删除平台车辆model]
  	 * @Author   孙丽男
  	 * @DateTime 2018-06-08
  	 * @param    [车辆id] arr $carIdArr
  	 * @version  6.2
  	 * @return   [type]     [description]
  	 */
  	public function deleteCarModel($carIdArr){
  		//删除之前查出平台车辆车牌号
  		$numObj= Db::table('car_car')
						->whereIn('id', $carIdArr)
						->select('num')
						->get();
		$num = json_decode(json_encode($numObj),true);

  		$deleteCar= Db::table('car_car')
						->whereIn('id', $carIdArr)
						->delete();
		if ($deleteCar) {	
			$deleteInsura= Db::table('insurance')
						->whereIn('cid', $carIdArr)
						->delete();

			$result['code'] = 1000;
    		$result['msg'] = '删除成功';
    		$result['num'] = $num;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}

		return $result;
  	}

  	/**
  	 * [deleteInsuraModel 删除保险]
  	 * @Author   孙丽男
  	 * @DateTime 2018-06-21
  	 * @param    [type]
  	 * @version  6.2
  	 * @return   [type]     [description]
  	 */
  	public function deleteInsuraModel($id){
  		$numObj= Db::table('car_insurance')
						->where('id', $id)
						->join('car_car','car_car.id','=','car_insurance.carid')
						->select('car_car.num')
						->get();
		$num = json_decode(json_encode($numObj),true);

  		$deleteCar= Db::table('car_insurance')
						->where('id', $id)
						->delete();
		if ($deleteCar) {	

			$result['code'] = 1000;
    		$result['msg'] = '删除成功';
    		$result['num'] = $num;

		}else{

			$result['code'] = 1004;
	    	$result['msg'] = '删除失败';
		}

		return $result;
  	}

  	/**
  	 * [updateCarModel 修改平台车辆]
  	 * @Author   孙丽男
  	 * @DateTime 2018-06-08
  	 * @param    [type]
  	 * @version  6.2
  	 * @return   [type]     [description]
  	 */
  	public function updateCarModel($car,$carId,$insura){

  		$data['modifier'] = session('username');
		$data['modtime'] = date('Y-m-d H:i:s');
		$update = Db::table('car_car')
				->where('id',$carId)
				->update($data);
		if ($update) {
			foreach ($insura as $key => $value) {
				$updateinsura = Db::table('insurance')
							->where('id',$value['id'])
							->update($value);
			}
			if ($addData) {

				$addinsura = Db::table('insurance')
							->insert($addData);
				if ($addinsura) {
					$result['code'] = 1000;
					$result['msg'] = '修改成功';
				}else{

					$result['code'] = 1004;
					$result['msg'] = '修改失败';
				}
			}
			$result['code'] = 1000;
			$result['msg'] = '修改成功';
		}else{

			$result['code'] = 1004;
			$result['msg'] = '修改失败';
		}
		return $result;
  	}
  	/*
    |--------------------------------------------------------------------------
    |数据同步部分
    |--------------------------------------------------------------------------
    */
   /**
    * [findSync 查询同步信息]
    * @Author   孙丽男
    * @DateTime 2018-06-21
    * @param    [type]
    * @version  6.2
    * @param    [type]     $typeid [description]
    * @return   [type]             [description]
    */
  	public function findSync($typeid){
  		$find = Db::table('car_type')
  					->where('car_type.id',$typeid)
					->join('car_level','car_level.id','=','car_type.level')
					->join('car_brand','car_brand.id','=','car_type.brandid')
					->select(['car_brand.name as brandname','car_level.name as levelname','car_type.name as typename','car_type.maxride','car_type.energy','car_type.displacement'])
					->first();
		$data = json_decode(json_encode($find),true);
		return $data;
  	}
   /*
    |--------------------------------------------------------------------------
    |添加、修改下拉选项数据展示部分
    |--------------------------------------------------------------------------
    */
    /**
    * [purchbrand 已采购车辆品牌]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [品牌父级id] int $pid  默认为0
    * @version  6.2
    * @return   [type]     [description]
    */
   public function purchBrandModel(){

   		$childObj = Db::table('purchase')
					->join('car_type','car_type.id','=','purchase.car_id')
					->join('car_brand','car_brand.id','=','car_type.brandid')
					->select(['car_brand.pid'])
					->get();
		$childArr = json_decode(json_encode($childObj),true);
		if ($childArr) {
			//取出品牌父级id，查询品牌信息
			foreach ($childArr as $key => $value) {
				$brandIdArr[$key] = $value['pid'];
			}

			$brandObj = Db::table('car_brand')
						->whereIn('id',$brandIdArr)
						->select(['id','name'])
						->get();
			$brandArr = json_decode(json_encode($brandObj),true);
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $brandArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		return json_encode($result);
   }
   /**
    * [purchbrand 已采购车辆子品牌]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [品牌父级id] int $pid  默认为0
    * @version  6.2
    * @return   [type]     [description]
    */
   public function purchChildModel($pid = 0){

   		$childObj = Db::table('purchase')
   					->where('car_brand.pid',$pid)
					->join('car_type','car_type.id','=','purchase.car_id')
					->join('car_brand','car_brand.id','=','car_type.brandid')
					->select(['car_brand.id','car_brand.name'])
					->get();
		$childArr = json_decode(json_encode($childObj),true);
		if ($childArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $childArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		return json_encode($result);
   }

   /**
    * [purchbrand 已采购车系]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [品牌id]  int $brandId
    * @version  6.2
    * @return   [type]     [description]
    */
   public function purchSeriesModel($brandId){

   		$seriesObj = Db::table('car_series')
					->where('car_series.brandid',$brandId)
					->join('car_type','car_type.sid','=','car_series.id')
					->join('purchase','purchase.car_id','=','car_type.id')
					->select(['car_series.id','car_series.name'])
					->get();
		$seriesArr = json_decode(json_encode($seriesObj),true);
		if ($seriesArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $seriesArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		return json_encode($result);
   }

   /**
    * [purchbrand 已采购车辆车型]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [type]
    * @version  6.2
    * @return   [type]     [description]
    */
   public function purchTypeModel($seriesId){

		$typeObj = Db::table('car_type')
					->where('sid',$seriesId)
					->join('purchase','purchase.car_id','=','car_type.id')
					->select(['car_type.id','car_type.name'])
					->get();
		$typeArr = json_decode(json_encode($typeObj),true);
		if ($typeArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $typeArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		return json_encode($result);
   }
   /**
    * [company 公司列表]
    * @Author   孙丽男
    * @DateTime 2018-06-08
    * @param    [type]
    * @version  6.2
    * @return   [type]     [description]
    */
   public function companyModel(){

   		$companyObj = Db::table('branch')
					->select(['id','company_name as companyname'])
					->get();
		$companyArr = json_decode(json_encode($companyObj),true);
		if ($companyArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$result['data'] = $companyArr;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		return json_encode($result);
   }
   /**
    * [areaModel 地区model]
    * @Author   孙丽男
    * @DateTime 2018-06-12
    * @param    [type]
    * @version  6.2
    * @return   [type]     [description]
    */
   public function areaModel(){
   		$regionObj = Db::table('region')
   					->where('code','<>',100000)
   					->select('code as id','fullName as value','parentCode')
					->get();
		$regionArr = json_decode(json_encode($regionObj),true);
		if ($regionArr) {
			
			$result['code'] = 1000;
			$result['msg'] = '查询成功';
			$comm = new Common;
			//处理数组
			$data = $comm->createTree($regionArr, 100000, 'id', 'parentCode');
			$result['data'] = $data;
		}else{

			$result['code'] = 1004;
			$result['msg'] = '查询失败';
		} 

		 return json_encode($result);
   }
}